package yscmd

import (
	"context"
	"gitee.com/kmyss/gf-ex/yslog"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/os/gproc"
	"sync"
)

var _fmtWG sync.WaitGroup

func GoDirFmt(dir string) {
	files, err := gfile.ScanDirFile(dir, "*.go", true)
	if err != nil {
		yslog.Error(err)
		return
	}

	_fmtWG.Add(len(files))
	for _, file := range files {
		go GoFileFmt(file, &_fmtWG)
	}
	_fmtWG.Wait()
}

func GoFileFmt(path string, group *sync.WaitGroup) {
	cmd := "go fmt " + path
	gproc.ShellRun(context.TODO(), cmd)
	if group != nil {
		group.Done()
	}
}
